package com.num2.service;

import com.num2.dao.IDemoDao;
import com.num2.form.DemoForm;
import com.num2.model.DemoModel;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DemoService {

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**登陆验证
     * method: login(LoginForm loginForm)
     * @param loginForm
     * @return
     */
    public DemoModel login(DemoForm loginForm){
        //打开session
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //通过 注解 获取 接口实现类的对象
        IDemoDao userDao = sqlSession.getMapper(IDemoDao.class);
        //获取结果
        List<DemoModel> loginModels = userDao.login(loginForm);
        //关闭会话
        sqlSession.close();
        if (loginModels.size() > 0)
            return loginModels.get(0);
        else
            return null;
    }

    public SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }

    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }
}